/*
 * Powered By www.cardmee.net
 */
package com.ns.school.service.wave.impl;

import com.ns.school.common.utils.DateUtil;
import java.util.List;
import java.util.Map;

import com.ns.school.po.edu.LayuiResult;
import com.ns.school.po.sys.User;
import com.ns.school.po.wave.WaveDevicePO;
import com.ns.school.po.wave.WaveRequestDetailPO;
import lombok.extern.slf4j.Slf4j;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ns.school.mapper.wave.WaveOrderRecordMapper;
import com.ns.school.po.wave.WaveOrderRecordPO;
import com.ns.school.common.base.R;
import com.ns.school.service.base.BaseService;
import com.ns.school.service.wave.WaveOrderRecordService;

/**
 * @author wt-97
 */
@Slf4j
@Service
public class WaveOrderRecordServiceImpl extends BaseService implements WaveOrderRecordService {

    @Autowired
    private WaveOrderRecordMapper waveOrderRecordMapper;

	/**
     * 通过id查询单个对象
     */
    @Override
    public WaveOrderRecordPO queryById(WaveOrderRecordPO params){
        return waveOrderRecordMapper.queryById(params);
    }

    /**
     * 通过id查询单个对象
     */
    @Override
    public WaveOrderRecordPO queryById(Integer id){
        return waveOrderRecordMapper.queryById(id);
    }

    /**
     * 通过id查询单个对象
     */
    @Override
    public void queryById(WaveOrderRecordPO params, R<WaveOrderRecordPO> r){
        r.setData(queryById(params));
    }

    /**
     * 通过条件查询单个对象
     */
    @Override
    public WaveOrderRecordPO queryOne(WaveOrderRecordPO params){
        return waveOrderRecordMapper.queryOne(params);
    }

    /**
     * 通过条件查询单个对象
     */
    @Override
    public void queryOne(WaveOrderRecordPO params, R<WaveOrderRecordPO> r){
        r.setData(queryOne(params));
    }

    /**
     * 查询全部
     */
    @Override
    public List<WaveOrderRecordPO> queryList(WaveOrderRecordPO params){
        return waveOrderRecordMapper.queryList(params);
    }

    /**
     * 查询全部(分页)
     */
    @Override
    public R<List<?>> queryPageList(WaveOrderRecordPO params){
        R<List<?>> r = new R<List<?>>();
        startPage(params);
        List<WaveOrderRecordPO> list = waveOrderRecordMapper.queryList(params);
        for (WaveOrderRecordPO cur : list) {
            cur.setEncryptionId(encode(cur.getId()));
        }
        r.setData(list);
        r.setCount(new PageInfo<>(list).getTotal());
        return r;
    }

    /**
     * 查询总数
     */
    @Override
    public int queryCount(WaveOrderRecordPO params){
        return waveOrderRecordMapper.queryCount(params);
    }

    /**
     * 新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> insert(WaveOrderRecordPO params)  {
        params.setCreateTime(DateUtil.getNowDate());
        if (waveOrderRecordMapper.insertSelective(params) == 0) {
            transactionRollBack();
            return R.error("新增失败");
        }
        return R.success();
    }

    /**
     * 批量新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> insertBatch(List<WaveOrderRecordPO> list) {
        if (waveOrderRecordMapper.insertBatch(list) == 0) {
            transactionRollBack();
            return R.error("批量新增失败");
        }
        return R.success();
    }

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> update(WaveOrderRecordPO params)  {
        //params.setUpdateTime(DateUtil.getNowDate());
        if (waveOrderRecordMapper.updateById(params) == 0) {
            transactionRollBack();
            return R.error("更新失败");
        }
        return R.success();
    }

    /**
     * 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> updateDel(WaveOrderRecordPO params)  {
        if (waveOrderRecordMapper.updateDel(params) == 0) {
            transactionRollBack();
            return R.error("删除失败");
        }
        return R.success();
    }

    @Override
    public List<WaveDevicePO> queryAllName(Map<String, Object> map) {
        return waveOrderRecordMapper.queryAllName(map);
    }

    @Override
    public List<User> selectAllNames(Map<String, Object> map) {
        return waveOrderRecordMapper.selectAllName(map);
    }

    @Override
    public WaveOrderRecordPO selectWaveOrder(Integer id) {
        return waveOrderRecordMapper.selectWaveOrder(id);
    }

    @Override
    public R<?> updateDelAll(WaveOrderRecordPO params, String ids) {
        String[] strarray = ids.split(",");
        //删除
        for (int i = 0; i < strarray.length; i++) {
            if (!strarray[i].isEmpty()) {
                if(waveOrderRecordMapper.updateDelAll(Integer.valueOf(strarray[i]))== 0){
                    transactionRollBack();
                    return R.error("删除失败");
                }
            }
        }
        return R.success();
    }

    @Override
    public int checkOrder(WaveOrderRecordPO waveOrderRecordPO) {
        return waveOrderRecordMapper.checkOrder(waveOrderRecordPO);
    }

    @Override
    public void auditOrder(LayuiResult result, Integer id) {
        int cont=waveOrderRecordMapper.checkOrderUpdate(result,id);
        if(cont>0){
            result.setCode(0);
            result.setMsg("审核通过");
        }
    }

    @Override
    public int orderRequestFail(Integer id) {
        return waveOrderRecordMapper.orderRequestFail(id);
    }

}
